<?php
define('EQUAL', 1);
define('NOT_EQUAL', 2);
define('GREATER', 3);
define('NOT_GREATER', 4);
define('LESS', 5);
define('NOT_LESS', 6);
define('IN', 7);
define('NOT_IN', 8);
define('LIKE', 9);
define('BETWEEN', 10);
define('IS_NULL', 11);
define('NOT_NULL', 12);
define('EXAMPLE', 13);
define('REGEXP', 14);

define('LIKE_ANYWHERE', 1);
define('LIKE_START', 2);
define('LIKE_END', 3);
define('LIKE_EXACT', 4);

class XCriterion{
	protected $field = null; 
	protected $type = null; 
	protected $value = null; 
	protected $alias = null;	
	protected $parentCriteria = null;
	
	public function __construct($field, $type, $value = null, $alias = null){
		$this->field = $field;
		$this->type = $type;
		$this->value = $value;
		$this->alias = $alias;
	}
	
	/**
	 * @return mixed
	 */
	public function field() {
		return $this->field;
	}

	/**
	 * @return mixed
	 */
	public function type() {
		return $this->type;
	}

	/**
	 * @return mixed
	 */
	public function value() {
		return $this->value;
	}

	/**
	 * @return mixed
	 */
	public function alias() {
		return $this->alias;
	}

	/**
	 * @return XCriteria
	 */
	public function parentCriteria() {
		return $this->parentCriteria;
	}
	
	/**
	 * @param XCriteria $parentCriteria
	 * @return XCriterion
	 */
	public function setParentCriteria(XCriteria $parentCriteria) {
		$this->parentCriteria = $parentCriteria;
		return $this;
	}
	
	/**
	 * @return XCriterion
	 */
	public static function example(XModel $example){
		return new self(get_class($example), EXAMPLE, $example);
	}
	
	/**
	 * @return XCriterion
	 */
	public function regexp($field, $value){
		return new self($field, REGEXP, $value);
	}

	/**
	 * @return XCriterion
	 */
	public static function eq($field, $value){
		return new self($field, EQUAL, $value);
	}
			
	/**
	 * @return XCriterion
	 */	
	public static function not($field, $value){
		return new self($field, NOT_EQUAL, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function greater($field, $value){
		return new self($field, GREATER, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function notGreater($field, $value){
		return new self($field, NOT_GREATER, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function less($field, $value){
		return new self($field, LESS, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function notLess($field, $value){
		return new self($field, NOT_LESS, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function in($field, array $value){
		return new self($field, IN, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function notIn($field, array $value){
		return new self($field, NOT_IN, $value);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function like($field, $value, $mode = LIKE_ANYWHERE){
		return new self($field, LIKE, $value, $mode);
	}
	
	/**
	 * @return XCriterion
	 */	
	public static function between($field, $from, $to){
		return new self($field, BETWEEN, $from, $to);
	}	
	
	/**
	 * @return XCriterion
	 */	
	public static function isNull($field){
		return new self($field, IS_NULL);
	}
	
	/**
	 * @return XCriterion
	 */
	public static function notNull($field){
		return new self($field, NOT_NULL);
	}
	
}
?>